<div style="padding-left:8px;">
    {{ paginator.links('pagination.bootstrap-4')|raw }}
</div>
<table class="table table-responsive table-hover" id="bill-sortable">
    <thead>
    <tr>
        <th class="hidden-sm hidden-xs">&nbsp;</th>
        <th class="hidden-sm hidden-xs">&nbsp;</th>
        <th>{{ trans('list.name') }}</th>
        <th class="hidden-sm hidden-md hidden-xs">{{ trans('list.linked_to_rules') }}</th>
        <th style="text-align: right;">{{ trans('list.matchingAmount') }}</th>
        <th class="hidden-sm hidden-xs">{{ trans('list.paid_current_period') }}</th>
        <th class="hidden-sm hidden-xs">{{ trans('list.next_expected_match') }}</th>
        <th class="hidden-sm hidden-xs">{{ trans('list.repeat_freq') }}</th>
    </tr>
    </thead>
    {% for objectGroupOrder, objectGroup in bills %}
        {% if objectGroup.bills|length > 0 %}
            <tbody class="bill-connected-list"
                   {% if objectGroupOrder != 0 %}data-title="{{ objectGroup.object_group_title }}"
                   {% else %}data-title=""{% endif %}>
            <tr>
                <td class="hidden-sm hidden-xs">&nbsp;</td>
                <td class="hidden-sm hidden-xs">&nbsp;</td>
                <td colspan="6"><small>{{ objectGroup.object_group_title }}</small></td>
            </tr>
            {% for entry in objectGroup.bills %}
                <tr class="bill-sortable" data-id="{{ entry.id }}" data-name="{{ entry.name }}"
                    data-order="{{ entry.order }}" data-position="{{ loop.index0 }}">
                    <td class="hidden-sm hidden-xs">
                        <span class="fa fa-fw fa-bars bill-handle"></span>
                    </td>
                    <td class="hidden-sm hidden-xs">
                        <div class="btn-group btn-group-xs edit_tr_buttons"><a href="{{ route('subscriptions.edit',entry.id) }}"
                                                                               class="btn btn-default btn-xs"><span
                                        class="fa fa-fw fa-pencil"></span></a><a
                                    href="{{ route('subscriptions.delete',entry.id) }}" class="btn btn-danger btn-xs"><span
                                        class="fa fa-fw fa-trash-o"></span></a></div>
                    </td>
                    <td>
                        {% if not entry.active %}
                            <span class="fa fa-fw fa-ban"></span>
                        {% endif %}
                        <a href="{{ route('subscriptions.show',entry.id) }}" title="{{ entry.name }}">{{ entry.name }}</a>
                        {# count attachments #}
                        {% if entry.attachments.count() > 0 %}
                            <span class="fa fa-paperclip"></span>
                        {% endif %}

                    </td>
                    <td class="hidden-sm hidden-md hidden-xs rules">
                        {% if entry.rules|length > 0 %}
                            <ul class="list-unstyled">
                                {% for rule in entry.rules %}
                                    <li>
                                        <a href="{{ route('rules.edit', [rule.id]) }}">
                                            {{ rule.title }}
                                        </a>
                                        {% if not rule.active %}({{ 'list_inactive_rule'|_|lower }}){% endif %}
                                    </li>
                                {% endfor %}
                            </ul>
                        {% endif %}
                    </td>
                    <td style="text-align: right;">
                <span style="margin-right:5px;"
                      title="{{ formatAmountBySymbol(entry.amount_min, entry.currency_symbol, entry.currency_decimal_places, false)|escape }} -- {{ formatAmountBySymbol(entry.amount_max, entry.currency_symbol, entry.currency_decimal_places, false)|escape }}"
                >
                    ~ {{ formatAmountBySymbol((entry.amount_max + entry.amount_min)/2, entry.currency_symbol, entry.currency_decimal_places) }}

                    {% if '0' != entry.native_amount_max %}
                    (~ {{ formatAmountBySymbol((entry.native_amount_max + entry.native_amount_min)/2, defaultCurrency.symbol, defaultCurrency.decimal_places) }})
                    {% endif %}
                </span>
                    </td>

                    {#
                    paidDates = 0 (bill not paid in period)
                    pay_dates  = 0 (bill not expected to be paid in this period)
                    bill is active.
                    #}
                    {% if entry.paid_dates|length == 0 and entry.pay_dates|length == 0 and entry.active %}
                        <td class="paid_in_period text-muted">
                            {{ trans('firefly.not_expected_period') }}
                        </td>
                        <td class="expected_in_period hidden-sm hidden-xs">
                            {% if entry.next_expected_match %}
                                {{ formatDate(entry.next_expected_match, monthAndDayFormat) }}
                            {% endif %}
                        </td>
                    {% endif %}

                    {#
                    paid_dates = 0 (bill not paid in period)
                    pay_dates  > 0 (bill IS expected to be paid in this period)
                    bill is active
                    first pay date is in the past.
                    #}
                    {% if entry.paid_dates|length == 0 and entry.pay_dates|length > 0 and entry.active %}
                        {% if entry.next_expected_match_diff == trans('firefly.not_expected_period') %} {# terrible code, you should sue me for this. #}
                            <td class="paid_in_period text-muted">
                                {{ entry.next_expected_match_diff }}
                            </td>
                        {% else %}
                            <td class="paid_in_period text-warning">
                                {{ entry.next_expected_match_diff }}
                                <!-- {{ trans('firefly.bill_expected_date', {date: entry.next_expected_match_diff }) }} -->
                            </td>
                        {% endif %}
                        <td class="expected_in_period hidden-sm hidden-xs">

                            {% for date in entry.pay_dates %}
                                {{ formatDate(date, monthAndDayFormat) }}<br>
                            {% endfor %}

                        </td>
                    {% endif %}

                    {#
                    paid_dates >= 0 (bill is paid X times).
                    Don't care about pay_dates.
                    #}
                    {% if entry.paid_dates|length > 0 and entry.active %}
                        <td class="paid_in_period text-success">

                            {% for currentPaid in entry.paid_dates %}
                                <a href="{{ route('transactions.show',currentPaid.transaction_group_id) }}">
                                    {{ formatDate(currentPaid.date, monthAndDayFormat) }}
                                </a>
                                <br/>
                            {% endfor %}
                        </td>
                        <td class="expected_in_period hidden-sm hidden-xs">
                            {% if entry.next_expected_match %}
                                    {{ formatDate(entry.next_expected_match, monthAndDayFormat) }}
                                {% else %}
                                    <span class="text-muted">{{ entry.next_expected_match_diff }}</span>
                            {% endif %}
                        </td>
                    {% endif %}
                    {# bill is not active #}
                    {% if not entry.active %}
                        <td class="paid_in_period text-muted">
                            ~
                        </td>
                        <td class="expected_in_period text-muted hidden-sm hidden-xs">~</td>
                    {% endif %}
                    <td class="hidden-sm hidden-xs">
                        {{ ('repeat_freq_'~entry.repeat_freq)|_ }}
                        {% if entry.skip > 0 %}
                            {{ 'skips_over'|_ }} {{ entry.skip }}
                        {% endif %}
                        {% if entry.end_date %}
                            <br>
                            {% if carbonize(entry.end_date).lte(today) %}
                                <span class="text-danger">
                                    {{ trans('firefly.bill_end_index_line', {date: carbonize(entry.end_date).isoFormat(monthAndDayFormat)  })  }}
                                    </span>
                            {% else %}
                                {{ trans('firefly.bill_end_index_line', {date: carbonize(entry.end_date).isoFormat(monthAndDayFormat)  })  }}
                            {% endif %}
                        {% endif %}
                        {% if entry.extension_date %}
                            <br>
                            {% if carbonize(entry.extension_date).lte(today) %}
                                <span class="text-danger">
                                    {{ trans('firefly.bill_extension_index_line', {date: carbonize(entry.extension_date).isoFormat(monthAndDayFormat)  })  }}
                                    </span>
                            {% else %}
                                {{ trans('firefly.bill_extension_index_line', {date: carbonize(entry.extension_date).isoFormat(monthAndDayFormat)  })  }}
                            {% endif %}
                        {% endif %}
                    </td>
                </tr>
            {% endfor %}
            {% for sum in sums[objectGroupOrder] %}
                {% if '0' != sum.avg %}
                    <tr>
                        <td class="hidden-sm hidden-xs">&nbsp;</td> <!-- handle -->
                        <td class="hidden-sm hidden-xs">&nbsp;</td> <!-- buttons -->
                        <td colspan="2" style="text-align: right;"> <!-- title -->
                            <small>{{ 'sum'|_ }} ({{ sum.currency_name }}) ({{ 'active_exp_bills_only'|_ }})</small>
                        </td>
                        <td style="text-align: right;"> <!-- amount -->
                            {{ formatAmountBySymbol(sum.avg, sum.currency_symbol, sum.currency_decimal_places) }}
                        </td>
                        <td>&nbsp;</td> <!-- paid in period -->
                        <td class="hidden-sm hidden-xs">&nbsp;</td> <!-- next expected match -->
                        <td class="hidden-sm hidden-xs">&nbsp;</td><!-- repeats -->
                    </tr>
                {% endif %}
                {% if '0' != sum.per_period %}
                    <tr>
                        <td class="hidden-sm hidden-xs">&nbsp;</td> <!-- handle -->
                        <td class="hidden-sm hidden-xs">&nbsp;</td> <!-- buttons -->
                        <td colspan="2" style="text-align: right;"> <!-- title -->
                            <small>{{ ('per_period_sum_'~sum.period)|_ }} ({{ sum.currency_name }})
                                ({{ 'active_bills_only'|_ }})</small>
                        </td>
                        <td style="text-align: right;"> <!-- amount -->
                            {{ formatAmountBySymbol(sum.per_period, sum.currency_symbol, sum.currency_decimal_places) }}
                        </td>
                        <td>&nbsp;</td> <!-- paid in period -->
                        <td class="hidden-sm hidden-xs">&nbsp;</td> <!-- next expected match -->
                        <td class="hidden-sm hidden-xs">&nbsp;</td><!-- repeats -->
                    </tr>
                {% endif %}
            {% endfor %}
            </tbody>
        {% endif %}
    {% endfor %}
    {% if totals|length > 0 %}
        <tfoot>
        <tr>
            <td colspan="8" style="border-top:1px #777 solid;"></td>
        </tr>
        {% for sum in totals %}
            {% if '0' != sum.avg %}
                <tr>
                    <td class="hidden-sm hidden-xs">&nbsp;</td> <!-- handle -->
                    <td class="hidden-sm hidden-xs">&nbsp;</td> <!-- buttons -->
                    <td colspan="2" style="text-align: right;"> <!-- title -->
                        <small>{{ 'sum'|_ }} ({{ sum.currency_name }}) ({{ 'active_exp_bills_only_total'|_ }})</small>
                    </td>
                    <td style="text-align: right;"> <!-- amount -->
                        {{ formatAmountBySymbol(sum.avg, sum.currency_symbol, sum.currency_decimal_places) }}
                    </td>
                    <td>&nbsp;</td> <!-- paid in period -->
                    <td class="hidden-sm hidden-xs">&nbsp;</td> <!-- next expected match -->
                    <td class="hidden-sm hidden-xs">&nbsp;</td><!-- repeats -->
                </tr>
            {% endif %}
            {% if '0' != sum.per_period %}
                <tr>
                    <td class="hidden-sm hidden-xs">&nbsp;</td> <!-- handle -->
                    <td class="hidden-sm hidden-xs">&nbsp;</td> <!-- buttons -->
                    <td colspan="2" style="text-align: right;"> <!-- title -->
                        <small>{{ ('per_period_sum_'~sum.period)|_ }} ({{ sum.currency_name }})
                            ({{ 'active_bills_only_total'|_ }})</small>
                    </td>
                    <td style="text-align: right;"> <!-- amount -->
                        {{ formatAmountBySymbol(sum.per_period, sum.currency_symbol, sum.currency_decimal_places) }}
                    </td>
                    <td>&nbsp;</td> <!-- paid in period -->
                    <td class="hidden-sm hidden-xs">&nbsp;</td> <!-- next expected match -->
                    <td class="hidden-sm hidden-xs">&nbsp;</td><!-- repeats -->
                </tr>
            {% endif %}
        {% endfor %}
        </tfoot>
    {% endif %}

</table>
<div style="padding-left:8px;">
    {{ paginator.links('pagination.bootstrap-4')|raw }}
</div>
